package jdbc_study.dbcp;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

/**
 * @author ChenYongHao
 * @date 2023/5/8 11:25
 * @desc dbcp-config.properties 配置文件
 */

public class DBCPWithConfig {

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws Exception {
        BasicDataSourceFactory dataSourceFactory = new BasicDataSourceFactory();
        Properties properties = new Properties();
        InputStream is = DBCPWithConfig.class.getClassLoader().getResourceAsStream("dbcp-config.properties");
        properties.load(is);
        DataSource dataSource = dataSourceFactory.createDataSource(properties);

        Connection connection = dataSource.getConnection();
        String sql = "select * from admin";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        ResultSet resultSet = prepareStatement.executeQuery();
        while (resultSet.next()) {
            Object id = resultSet.getObject(1);
            Object username = resultSet.getObject(2);
            Object password = resultSet.getObject(3);
            System.out.println(id + ":" + username + ":" + password);
        }
        resultSet.close();
        connection.close();
    }

}

